package org.bingluo.livewallpaper.hunterxhunter;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import cn.waps.AppConnect;

public class MainActivity extends Activity implements OnClickListener{

	private Button btnUseNow;
	private Button btnSetting;
	private Button btnSpread;
	
	private static final String ACTION_UNINSTALL_SHORTCUT =   
			"com.android.launcher.action.UNINSTALL_SHORTCUT"; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		buildComponents();
		
		delShortcut(this);
//		Intent intent = new Intent(ACTION_UNINSTALL_SHORTCUT );  
//		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
//		ComponentName comp = new ComponentName(info.activityInfo.packageName,
//				info.activityInfo.name);
//		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent()
//				.setComponent(comp).setAction("android.intent.action.MAIN"));
//		sendBroadcast(intent);
		////////////////初始化ad++++++++++++
		
		////////////////初始化ad------------
	}
	
	/**
	 * 删除当前应用的桌面快捷方式
	 * 
	 * @param cx
	 */
	public static void delShortcut(Context cx) {
	    Intent shortcut = new Intent(
	            "com.android.launcher.action.UNINSTALL_SHORTCUT");
//	    Toast.makeText(cx, "删除桌面快捷", Toast.LENGTH_SHORT).show();
	    // 获取当前应用名称
	    String title = null;
	    try {
	        final PackageManager pm = cx.getPackageManager();
	        title = pm.getApplicationLabel(
	                pm.getApplicationInfo(cx.getPackageName(),
	                        PackageManager.GET_META_DATA)).toString();
	    } catch (Exception e) {
	    }
	    // 快捷方式名称
	    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
	    Intent shortcutIntent = cx.getPackageManager()
	            .getLaunchIntentForPackage(cx.getPackageName());
	    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
	    cx.sendBroadcast(shortcut);
	}

	private void buildComponents() {
		btnUseNow = (Button) findViewById(R.id.btnUseNow);
		btnUseNow.setOnClickListener(this);
		btnSetting = (Button) findViewById(R.id.btnSetting);
		btnSetting.setOnClickListener(this);
		btnSpread = (Button) findViewById(R.id.btnSpread);
		btnSpread.setOnClickListener(this);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnUseNow:
			Intent intents = new Intent();
			intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intents.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
			startActivity(intents);
			Toast.makeText(getApplicationContext(), "请选择【"+getString(R.string.app_name)+"】", Toast.LENGTH_LONG).show();
			finish();
			break;
		case R.id.btnSetting:
			Intent intentSetting = new Intent(this, MainWallpaperServiceSettings.class);
//			intentSetting.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			intentSetting.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
			startActivity(intentSetting);
			break;
		case R.id.btnSpread:
			AppConnect.getInstance(getApplicationContext()).showOffers(getApplicationContext());
			break;

		default:
			break;
		}
	}

}
